<!-- 收货检验管理 -->
<template>
  <div class="mod-config">
    <el-button-group style="margin-bottom: 10px;">
      <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
      <el-button type="primary" icon="el-icon-delete" @click="searchReset">清空</el-button>
      <el-button v-if="permissions.eims_inventorycheck_add" icon="el-icon-plus" type="primary" @click="addInlensHandle()">收货检验非镜片类新增</el-button>
      <el-button v-if="permissions.eims_inventorycheck_add" icon="el-icon-plus" type="primary" @click="addLensHandle()">收货检验镜片类新增</el-button>
    </el-button-group>
    <el-form inline label-width="100px" @keyup.enter.native="searchHandle">
      <el-form-item label="检查单号">
        <el-input v-model="dataForm.linkBillId"></el-input>
      </el-form-item>
      <el-form-item label="制单人">
        <el-input v-model="dataForm.createUserName"></el-input>
      </el-form-item>
      <el-form-item label="单据日期">
        <el-row>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.createTimeStart" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
          <el-col class="line" :span="2" style="text-align: center;">-</el-col>
          <el-col :span="11">
            <el-date-picker v-model="dataForm.createTimeEnd" style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="检查类型">
        <el-select v-model="dataForm.checkType">
          <el-option value="0" label="镜片"></el-option>
          <el-option value="1" label="非镜片"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="dataForm.viewGoodsName"></el-input>
      </el-form-item>
      <el-form-item label="制造商">
        <el-input v-model="dataForm.supplyName">
          <el-button slot="append" @click="choiceSupply">选择</el-button>
        </el-input>
      </el-form-item>
    </el-form>
    <div class="avue-crud">
      <el-table :data="dataList" border v-loading="dataListLoading" :max-height="tableHeight">
        <el-table-column prop="billId" label="单号" width="160"></el-table-column>
        <el-table-column prop="linkBillId" label="收货单单号" width="160"></el-table-column>
        <el-table-column prop="inStockName" label="入库仓库" width="140"></el-table-column>
        <el-table-column prop="supplyName" label="制造商" width="160"></el-table-column>
        <el-table-column prop="checkStatus" label="收货检验状态" :formatter="statusFormatter" width="100"></el-table-column>
        <el-table-column prop="checkType" label="收货检验类型" :formatter="typeFormatter" width="100"></el-table-column>
        <el-table-column prop="memo" label="订单备注" show-overflow-tooltip></el-table-column>
        <el-table-column prop="createUserName" label="创建人员姓名" width="100"></el-table-column>
        <el-table-column label="操作" width="140" fixed="right">
          <template slot-scope="scope">
            <el-button v-if="permissions.eims_inventorycheck_edit" type="text" icon="el-icon-edit" @click="updateHandle(scope.row.id,scope.row.checkType)">修改</el-button>
            <el-button v-if="permissions.eims_inventorycheck_del" type="text" icon="el-icon-delete" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <lens-form v-if="lensFormVisible" ref="lensForm" @refreshDataList="getDataList"></lens-form>
    <inlens-form v-if="inlensFormVisible" ref="inlensForm" @refreshDataList="getDataList"></inlens-form>
    <search-supply v-if="supplyVisible" ref="supplyDialog" @refreshData="getSupplyData"></search-supply>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { filterParams } from "@/util/tools"
  import { receiveTestFetchList, receiveTestDelObj } from '@/api/eims/purchase/receive-test'

  import LensForm from './lens-form'
  import InlensForm from './inlens-form'
  import SearchSupply from '@/components/business/SearchSupply'

  export default {
    data() {
      return {
        dataForm: {
          supplyName: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 200,
        dataListLoading: false,
        addOrUpdateVisible: false,
        lensFormVisible: false,
        inlensFormVisible: false,
        supplyVisible: false
      }
    },
    components: {
      LensForm,
      InlensForm,
      SearchSupply
    },
    created() {
      this.getDataList()
    },
    computed: {
      ...mapGetters(['permissions']),
      tableHeight: function(){
        return window.innerHeight - 260
      }
    },
    methods: {
      //  查询数据
      searchHandle() {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        let params1 = {
          current: this.pageIndex,
          size: this.pageSize
        };
        let params2 = filterParams(this.dataForm);
        receiveTestFetchList(Object.assign(params1, params2)).then(response => {
          if(response.data.code == '0'){
            this.dataList = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
          this.dataListLoading = false
        })
      },
      //清空
      searchReset() {
        for (var key in this.dataForm) {
          this.dataForm[key] = ''
        }
      },
      // 镜片类新增
      addLensHandle(id) {
        this.lensFormVisible = true
        this.$nextTick(() => {
          this.$refs.lensForm.init(id)
        })
      },
      // 非镜片类新增
      addInlensHandle(id) {
        this.inlensFormVisible = true
        this.$nextTick(() => {
          this.$refs.inlensForm.init(id)
        })
      },
      //  修改
      updateHandle(id, type) {
        if (type == '1') {
          this.addLensHandle(id)
        } else {
          this.addInlensHandle(id)
        }
      },
      // 删除
      deleteHandle(id) {
        this.$confirm('是否确认删除ID为' + id, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function() {
          return receiveTestDelObj(id)
        }).then(response => {
          if(response.data.code == '0'){
            this.$message.success('删除成功')
            this.getDataList()
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      //  选择制造商
      choiceSupply() {
        this.supplyVisible = true
        this.$nextTick(() => {
          this.$refs.supplyDialog.init({
            ctgId: ''
          })
        })
      },
      getSupplyData(data) {
        this.dataForm.supplyId = data.id
        this.dataForm.supplyName = data.name
      },
      statusFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case "0":
            return '未审核'
            break
          case "1":
            return '已审核'
            break
        }
      },
      typeFormatter(row, column, cellValue, index) {
        switch (cellValue) {
          case "0":
            return '非镜片类'
            break
          case "1":
            return '镜片类'
            break
        }
      }
    }
  }
</script>
